package cn.com.shopec.erp.system.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.DateUtil;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.system.common.ParameterVoConvertor;
import cn.com.shopec.erp.system.common.UserUtils;
import cn.com.shopec.erp.system.dao.IParamsDao;
import cn.com.shopec.erp.system.entity.Parameter;
import cn.com.shopec.erp.system.service.IParamsService;
import cn.com.shopec.erp.system.vo.ParameterVo;

@Service
public class ParamsServiceImpl implements IParamsService{

	@Resource
	private IParamsDao iparamsDao; 
	
	@Resource
	private ParameterVoConvertor parameterVoConvertor;
	
	/**
	 * 
	 * @MethodName getPagedParamsVos
	 * @Description 获得分页后的参数配置Vo集合
	 * @author Liang Shun Wang
	 * @date 2013-5-30 下午4:48:50
	 * @param ParameterVo
	 * @param pageQuery
	 * @return PageFinder<ParameterVo>
	 * @throws Exception
	 */
	@Override
	public PageFinder<ParameterVo> getPagedParamsVos(ParameterVo parameterVo, PageQuery pageQuery) throws Exception {
		 DetachedCriteria dc = this.iparamsDao.createCriteria();
		 if (LogicUtil.isNotNull(parameterVo)) {
	            if (LogicUtil.isNotNullAndEmpty(parameterVo.getParamName())) {
	                dc.add(Restrictions.like("paramName", "%" + parameterVo.getParamName().trim() + "%"));
	            }
	        }
	        dc.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
	        // 按添加时间倒序
	        dc.addOrder(Order.desc("createTime"));
		 
	        PageFinder<Parameter> pagedBrandList = this.iparamsDao.pagedByCriteria(dc, pageQuery.getPage(),pageQuery.getPageSize());
	        return this.parameterVoConvertor.convert2PagedBrandVos(pagedBrandList);
	}

	/**
     * 
     * @Title: addBrand
     * @Description: 添加系统配置参数
     * @param brandVo
     * @throws
     * @author: duhui
     * @date: 2013-5-26下午04:15:58
     */
	@Override
	public void addParameter(ParameterVo parameterVo) throws Exception {
		Parameter parameter = this.parameterVoConvertor.convert2Brand(parameterVo);
		parameter.setCreateTime(DateUtil.formatTimesTampDate(new Date()));
		parameter.setParamUser(UserUtils.getLoginUserName());
		iparamsDao.save(parameter);
	}

	/**
     * 
     * @Title: getBrand
     * @Description: 根据编号查询系统参数信息
     * @param Id
     * @throws Exception
     * @throws
     * @author: LiangShunWang
     * @date: 2016-11-30下午05:02:06
     */
	@Override
	public ParameterVo getParameterVo(String id) throws Exception {
		return this.parameterVoConvertor.convert2BrandVo(this.iparamsDao.get("id", id));
	}

	/**
     * 
     * @Title: updateParamet
     * @Description: 修改系统参数
     * @param parameterVo
     * @throws Exception
     * @throws
     * @author: LiangShunWang
     * @date: 2016-11-30下午05:02:06
     */
	@Override
	public void updateParameter(ParameterVo parameterVo) throws Exception {
		if (LogicUtil.isNotNull(parameterVo)) {
        	Parameter parameter = this.iparamsDao.get("id",parameterVo.getId());
        	this.iparamsDao.update(this.parameterVoConvertor.convert2Brand(parameterVo, parameter));
        }
		
	}

	/**
     * 
     * @Title: delParameter
     * @Description: 删除系统参数
     * @param parameterVo
     * @throws Exception
     * @throws
     * @author: LiangShunWang
     * @date: 2016-11-30下午05:02:06
     */
	@Transactional
	@Override
	public void delParameter(String id) throws Exception {
		if(LogicUtil.isNotNull(id)){
			 Parameter parameter = this.iparamsDao.get("id", id);
			 parameter.setDeleteFlag(SZSCMConstant.DELETE_FLAG_YES);
			 parameter.setUpdater(UserUtils.getLoginUserRealName());
			this.iparamsDao.update(parameter);
		}
		
	}
	

}
